package pac1_rgr;

import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import queues.QueueForTransactions;
import widgets.ChooseRandom;

public class Workers extends Actor {
	private QueueForTransactions queueOfCars;
	private ChooseRandom rnd;
	private Main gui;
	private Model model;
	private double finishTime;
	public boolean time;
	private QueueForTransactions queueOfWorkers_outTime;

	public Workers(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		finishTime = gui.getChooseDataModelTime().getDouble();
		this.rnd = gui.getChooseRandomWork();
		this.gui = gui;
		this.model = model;
		queueOfCars = model.getQueueOfCars();
		queueOfWorkers_outTime = model.getQueueTimeWork();
		
	}

	@Override
	protected void rule() {
		IWaitCondition notEmptyQueue = new IWaitCondition() {

			@Override
			public boolean testCondition() {
				// TODO Auto-generated method stub
				return queueOfCars.size() > 0;
			}
			
		};
		
		
		
		while (getDispatcher().getCurrentTime()<finishTime){
			try {
				waitForCondition(notEmptyQueue);
			} catch (DispatcherFinishException e) {
				return;
			}
			queueOfCars.removeFirst();//грузчик берет товар
			gui.getHisto1().add(queueOfCars.size());
			time=model.getTimeTimer2().workTime; // берем и проверяем время(рабочее или нет)
			if (time == false){
				queueOfWorkers_outTime.addLast(time);// если время не рабочее = добавляем одного рабочего а список сверхурочников
				gui.getDiscretHisto().add(queueOfWorkers_outTime.size());
			}
			if (time == true){	//обнуляем список
				while(queueOfWorkers_outTime.size()>0){
					queueOfWorkers_outTime.removeFirst();
					gui.getDiscretHisto().add(queueOfWorkers_outTime.size());
				}
			}
			
			holdForTime(rnd.next());
			
		}		
			
		};

	
		
	}


